### REPLICATION FILE -- ANALYSIS, Germany
### Jonathan Homola & Margit Tavits
### "Contact Reduces Immigration-Related Fears for Leftist
### but Not for Rightist Voters"
### Comparative Political Studies

## clear environment, set seed, install/load packages
rm(list=ls())
set.seed(12435); options(stringsAsFactors=F)
# install.packages("stargazer")
library("stargazer")

## set working directory and source interaction functions
setwd(" ... ")
source("HomolaTavits_ContactFears_interactions.R")

## read in the main dataset
data <- read.csv("HomolaTavits_ContactFears_Germany.csv")

## recode "not finished school" from 5 to 1
data$education[data$education==5] <- 1

# replace missing data codes with NA
is.na(data) <- data==666
is.na(data) <- data==777
is.na(data) <- data==977

## create contact factor score
contact <- prcomp(~ as.numeric(contact1) + as.numeric(contact2) + as.numeric(contact3), 
                  data=data, na.action = na.exclude)
data$contact <- contact$x[,1]

## Eigenvalue of first and second factor
(contact$sdev^2*3/sum(contact$sdev^2))[1]  ## 1.62
(contact$sdev^2*3/sum(contact$sdev^2))[2]  ## 0.75

## create threat factor score
threat <- prcomp(~ as.numeric(violence_com) + as.numeric(violence_nat) + 
                   as.numeric(econ_hh) + as.numeric(econ_nat) +
                   as.numeric(nat_id) + as.numeric(ger_cult),
                 data=data, na.action = na.exclude)
data$threat <- -threat$x[,1]

## Eigenvalue of first and second factor
(threat$sdev^2*6/sum(threat$sdev^2))[1]  ## 4.28
(threat$sdev^2*6/sum(threat$sdev^2))[2]  ## 0.54

## Left/Right coding
## right vote2013 (1=CDU, 5=FDP, 6=AFD)
## left vote2013 (2=SPD, 3=Linke, 4=Greens)
## NA (7=Others, 8=no vote, 9=NA)
data$right <- NA
data$right[data$vote2013==1] <- 1
data$right[data$vote2013==5] <- 1
data$right[data$vote2013==6] <- 1
data$right[data$vote2013==2] <- 0
data$right[data$vote2013==3] <- 0
data$right[data$vote2013==4] <- 0

## continuous Left/Right (Manifesto scores) coding
## 1 - CDU 5.13
## 2 - SPD 3.82
## 3 - Left 3.27
## 4 - Greens 4.02
## 5 - FDP 5.70
## 6 - AFD 4.86
## NA (7=Others, 8=no vote, 9=NA)
data$lrscale <- NA
data$lrscale[data$vote2013==1] <- 5.13
data$lrscale[data$vote2013==2] <- 3.82
data$lrscale[data$vote2013==3] <- 3.27
data$lrscale[data$vote2013==4] <- 4.02
data$lrscale[data$vote2013==5] <- 5.70
data$lrscale[data$vote2013==6] <- 4.86

## subset to voters only
data2 <- data[!is.na(data$right),]




#### Table 3: The Effect of Contact and Left-Right Affinity on Immigration-Related
#### Threat, Germany

mod1 <- lm(threat ~ contact + as.factor(female) + age + education + income + as.factor(east), data2)
mod2 <- lm(threat ~ contact*right + as.factor(female) + age + education + income + as.factor(east), data2)
mod3 <- lm(threat ~ contact*lrscale + as.factor(female) + age + education + income + as.factor(east), data2)

stargazer(mod1, mod2, mod3,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.05, 0.01, NA), 
          column.sep.width="1pt", font.size="small", digits=2, 
          dep.var.caption="Outcome variable: Threat factor score",
          dep.var.labels = c("Contact only", "Left/Right",
                             "CMP scores"))




#### Figure 2: Marginal Effect of Contact on Threat Perceptions in Germany

#pdf("Fig2.pdf", height=6,width=12)
par(mfrow=c(1,2))
interaction_plot_binary(mod2, "contact", "right", "contact:right",
                        xlabel="Marginal Effect of Contact",
                        ylabel="",
                        ymax=0.5, ymin=-0.6,
                        factor_labels=c("Leftist", "Rightist"),
                        title="Threat (score)")
interaction_plot_continuous(mod3, "contact", "lrscale", "contact:lrscale",
                            rugplot=F, histogram=T,
                            ylabel="Marginal Effect of Contact",
                            xlabel="Continuous Left-Right Score",
                            ymax=0.6, ymin=-0.8,
                            title="Threat (score)")
par(mfrow=c(1,1))
#dev.off()




### CHAPEL HILL DATA
## Chapel Hill: Position on Immigration Policy 
## (0 opposed to restrictions -- 10 favor restrictions)
## 1 - CDU 5.727273
## 2 - SPD 3.909091
## 3 - Left 4.0
## 4 - Greens 2.090909
## 5 - FDP 3.6
## 6 - AFD 9.3
## NA (7=Others, 8=no vote, 9=NA)
data$chimmig<- NA
data$chimmig[data$vote2013==1] <- 5.727273
data$chimmig[data$vote2013==2] <- 3.909091
data$chimmig[data$vote2013==3] <- 4.0
data$chimmig[data$vote2013==4] <- 2.090909
data$chimmig[data$vote2013==5] <- 3.6
data$chimmig[data$vote2013==6] <- 9.3




#### Table 4: The Effect of Contact and Anti-Immigration Rhetoric 
#### on Immigration-Related Threat, Germany

mod1 <- lm(threat ~ contact*chimmig + as.factor(female) + age + education + income + as.factor(east), data)
stargazer(mod1,
          omit.stat=c("f", "ser", "bic", "ll", "adj.rsq"), 
          star.cutoffs = c(0.05, 0.01, NA), 
          column.sep.width="1pt", font.size="small", digits=2, 
          dep.var.caption="Outcome variable:",
          dep.var.labels = "Threat Factor Score")